package diva.pap;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import diva.android_library.ActivityPresenter;
import diva.android_library.rpc.Response;
import diva.lib.event.Event;
import diva.lib.event.Listener;
import diva.pap.model.Application;

public class TraficPresenter extends ActivityPresenter {

  protected final Application application = Application.getInstance();

  public TraficPresenter(Activity activity) {
    super(activity);
  }

  public void onCreate(Bundle state) {
    setContentView(R.layout.trafic_overview);
    initializeReload();
    getToggleButton(R.id.button_today).setOnCheckedChangeListener(new OnCheckedChangeListener() {
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) onTodayChecked();
      }
    });
    getToggleButton(R.id.button_thismonth).setOnCheckedChangeListener(
        new OnCheckedChangeListener() {
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) onThisMonthChecked();
          }
        });
    getToggleButton(R.id.button_today).setChecked(true);
    onTodayChecked();
  }

  protected void load() {
    boolean todaySelected = getToggleButton(R.id.button_today).isChecked();
    new TraficRequest(activity, todaySelected, new Response<TraficRequest>() {
      public void onReuestDone(TraficRequest data) {
        getTextView(R.id.chargebacksapproved_value).setText(data.getChargebacksapproved());
        // getTextView(R.id.chargebackspaid_value).setText(data.getChargebackspaid());
        getTextView(R.id.chargebackspending_value).setText(data.getChargebackspending());
        getTextView(R.id.clicksraw_value).setText(data.getClicksraw());
        getTextView(R.id.clicksunique_value).setText(data.getClicksunique());
        getTextView(R.id.commisionsapproved_value).setText(data.getCommisionsapproved());
        // getTextView(R.id.commisionspaid_value).setText(data.getCommisionspaid());
        getTextView(R.id.commisionspending_value).setText(data.getCommisionspending());
        getTextView(R.id.impressionsraw_value).setText(data.getImpressionsraw());
        getTextView(R.id.impressionsunique_value).setText(data.getImpressionsunique());
        getTextView(R.id.refundsapproved_value).setText(data.getRefundsapproved());
        // getTextView(R.id.refundspaid_value).setText(data.getRefundspaid());
        getTextView(R.id.refundspending_value).setText(data.getRefundspending());
        getTextView(R.id.salesapproved_value).setText(data.getSalesapproved());
        getTextView(R.id.salescostapproved_value).setText(data.getSalescostapproved());
        // getTextView(R.id.salescostpaid_value).setText(data.getSalescostpaid());
        getTextView(R.id.salescostpending_value).setText(data.getSalescostpending());
        // getTextView(R.id.salespaid_value).setText(data.getSalespaid());
        getTextView(R.id.salespending_value).setText(data.getSalespending());
      };

      public void onReuestFailed() {}
    }).send();
  }

  private void initializeReload() {
    getButton(R.id.button_reload).setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        Application.getInstance().reload();
      }
    });
    attach(Application.getInstance().getEventReload(), new Listener<Void>() {
      public void onEvent(Event<Void> event, Void arg) {
        load();
      }
    });
  }

  private void onThisMonthChecked() {
    getToggleButton(R.id.button_thismonth).setClickable(false);
    getToggleButton(R.id.button_today).setChecked(false);
    getToggleButton(R.id.button_today).setClickable(true);
    load();
  }

  private void onTodayChecked() {
    getToggleButton(R.id.button_today).setClickable(false);
    getToggleButton(R.id.button_thismonth).setChecked(false);
    getToggleButton(R.id.button_thismonth).setClickable(true);
    load();
  }
}
